const {
    User
} = require('../../../model/User');
// hash密码
const bcrypt = require('bcryptjs');
// 工具
const _ = require('lodash');

function getNewIntern() {
    let account = "eki" + Date.now();
    let password = randomString(12);
    return {
        account,
        motoPwd: password,
        password,
        nickname: 'Baby Panda',
        role: 15,
        avatar: null,
        status: true
    };
}

function randomString(len) {
    len = len || 16;
    let $chars = "iychlnoslekduaym395247081";
    let maxPos = $chars.length;
    let pwd = "";
    for (let i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}

module.exports = async (req, res) => {
    let intern = getNewIntern();
    const salt = await bcrypt.genSalt(10);
    intern.password = await bcrypt.hash(intern.password, salt);
    // 创建用户
    let user = new User(intern);
    // 保存用户
    await user.save();
    // 响应
    const resData = _.pick(user, ['_id', 'account', 'nickname', 'role', 'avatar', 'createTime', 'status']);
    res.sendResult(resData, 200, '登录成功');
}